/*
 * Copyright (C) 2011 by Fabien Barbero
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.flickr.api.photos;

import com.flickr.api.IdentifiedObject;
import com.flickr.api.LocalCache;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Fabien Barbero
 */
public class Photo implements IdentifiedObject {
    
    @XmlAttribute(name="id")
    private String id;
    
    @XmlAttribute(name="owner")
    private String owner;
    
    @XmlAttribute(name="secret")
    private String secret;
    
    @XmlAttribute(name="server")
    private String server;
    
    @XmlAttribute(name="username")
    private String username;
    
    @XmlAttribute(name="title")
    private String title;
    
    @XmlAttribute(name="farm")
    private int farm;
    
    @XmlAttribute(name="ispublic")
    private int ispublic;
    
    @XmlAttribute(name="isfriend")
    private int isfriend;
    
    @XmlAttribute(name="isfamily")
    private int isfamily;
    
    @XmlAttribute(name="thumb")
    private URL thumb;
    
    @XmlAttribute(name="url")
    private String webPage;
    
    @XmlTransient
    private LocalCache cache;
    
    void setCache(LocalCache cache) {
        this.cache = cache;
    }
    
    Photo() {}
    
    public static Photo create(String id, String server, int farm, String secret) {
        Photo photo = new Photo();
        photo.id = id;
        photo.server = server;
        photo.farm = farm;
        photo.secret = secret;
        return photo;
    }
    
    public static Photo create(String id) {
        Photo photo = new Photo();
        photo.id = id;
        return photo;
    }
    
    /**
     * Get the identifier of the photo.
     * @return The identifier.
     */
    @Override
    public String getId() {
        return id;
    }

    /**
     * Get the owner of the photo.
     * @return The owner.
     */
    public String getOwner() {
        return owner;
    }

    /**
     * Get the secret.
     * @return The secret.
     */
    public String getSecret() {
        return secret;
    }

    /**
     * Get the server.
     * @return The server.
     */
    public String getServer() {
        return server;
    }

    /**
     * Get the title of the photo.
     * @return The title.
     */
    public String getTitle() {
        return title;
    }

    /**
     * Get the user name who own the photo.
     * @return The user name.
     */
    public String getUserName() {
        return username;
    }
    
    /**
     * Indicates if the photo is public.
     * @return true if the photo is public.
     */
    public boolean isPublic() {
        return ispublic == 1;
    }
    
    /**
     * Indicates if the photo owns to a friend.
     * @return true if the photo owns to a friend.
     */
    public boolean isFriend() {
        return isfriend == 1;
    }
    
    /**
     * Indicates if the photo owns to a member of the user family.
     * @return true if the photo owns to a member of the user family.
     */
    public boolean isFamily() {
        return isfamily == 1;
    }
    
    /**
     * Get the thumbnail of the photo if available.
     * @return              The thumbnail or null.
     * @throws IOException  Error reading thumbnail.
     */
    public BufferedImage getThumbnail() throws IOException {
        return cache.getImage(thumb);
    }

    @Override
    public String toString() {
        return title;
    }
    
}
